package com.chonor.mall.controller;

import com.chonor.mall.common.CommonResult;
import com.chonor.mall.dto.OssPolicyResult;
import com.chonor.mall.service.UploadService;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@Api(tags = "UploadController", description = "图片上传OSS")
@RequestMapping("/aliyun/oss")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @ApiOperation(value = "oss上传签名生成")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult upload(@RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadFile(file);
        if(!Strings.isNullOrEmpty(url)){
            return CommonResult.success(url);
        }
        return CommonResult.failed("上传失败");
    }
}
